package poker;

import java.util.Arrays;
import java.util.Collections;

public class Deck 
{
	private Card[] deck;
	
	/*
	 * Keeps track of where we are in the deck
	 */
	int deckCounter;
	
	public Deck(Card[] startCards)
	{
		deck = new Card[50];
		createDeck(startCards, true);
		deckCounter = 0;
	}
	
	public Deck()
	{
		deck = new Card[52];
		createDeck();
		deckCounter = 0;
	}
	
	private void createDeck()
	{
		createDeck(null, false);
	}

	private void createDeck(Card[] startCards, boolean removeStartCards)
	{
		int cardPosition = 0;
		char suit = 'x';
		
		for(int i = 1; i < 5; i++)
		{
			if(i == 1) { suit = 's';}
			else if(i == 2) {suit = 'h';}
			else if(i == 3) {suit = 'c';}
			else if(i == 4) {suit = 'd';}
		
			for(int j = 2; j < 15; j++)
			{
				if(removeStartCards && startCards[0].getSuit() == suit && startCards[0].getValue() == j ||
				   removeStartCards && startCards[1].getSuit() == suit && startCards[1].getValue() == j)
					continue;
				
				Card card = new Card(suit, j);				
				deck[cardPosition] = card;
				cardPosition++;				
			}
		}	
		Collections.shuffle(Arrays.asList(deck));
	}
	
	public Card[] getDeck() {return deck;}
	
	public Card getNextCard() 
	{
		Card card = deck[deckCounter];
		deckCounter++;
		
		return card;
	}
	
	public String toString()
	{
		String res = "";
		for (int i = 0; i < deck.length; i++) 
		{
			res += deck[i] + "\n";
		}
		return res;
	}
	
	public static Card[] getCardsWithout(Card[] cards)
	{
		Card[] returnCards = new Card[52 - cards.length];
		
		int cardPosition = 0;
		char suit = 'x';
		for(int i = 1; i < 5; i++)
		{
			if(i == 1) { suit = 's';}
			else if(i == 2) {suit = 'h';}
			else if(i == 3) {suit = 'c';}
			else if(i == 4) {suit = 'd';}
		
			for(int j = 2; j < 15; j++)
			{

				Card card = new Card(suit, j);		
				if(ListUtils.contains(cards, card))
					continue;
						
				returnCards[cardPosition] = card;
				cardPosition++;				
			}
		}
		return returnCards;
	}
}
